<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="domain.entities.interfaces.*"%>
<%@ page import="domain.dao.interfaces.*"%>
<%@page import="util.*"%>
<%@page import="java.util.List"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="nested" uri="http://struts.apache.org/tags-nested"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<title>Show All Contact</title>

<link rel="stylesheet" type="text/css" href="/mdoc-project/css/bootstrap_Boris.css">
<base href="<%=request.getScheme()%>://<%=request.getServerName()%>:<%=request.getServerPort()%><%=request.getContextPath()%>/">
</head>
<body>
	<%
		boolean connected = session
					.getAttribute(DomainUtils.CURRENT_USER_STRING) != null;

			if (connected == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"You must be connected");
			}
	%>

	<%@include file="/pages/common_style/header.jsp"%>
	<%@include file="/pages/common_style/alerts.jsp"%>

	<%
		if (connected) {
	%>

	<html:errors />
	<br>
	<html:form action="/SearchContact">
		<div class="form-group">
			<label for="keywords">Search (Enter space-separeted keywords concerning wanted contact)</label>
			<input type="text" class="form-control" id="keywords" value="<%=DomainUtils.getNotNullValue(request,
								"keywords")%>" placeholder="Enter keywords" name="keywords">
			<br>
			<button type="submit" class="btn btn-default">Submit</button>
			<button type="reset" class="btn btn-default">Reset</button>
		</div>
	</html:form>
	<br>
	<br>

	<%
		ApplicationContext context = util.SpringUtils
						.getApplicationContext();
				IDAOContact daoContact = (IDAOContact) context
						.getBean("beanDAOContact");

				List<IContact> contacts = (List<IContact>) request
						.getAttribute(DomainUtils.SEARCH_CONTACT_RESULT);

				if (contacts == null) {
					contacts = daoContact.getAll();
				}
	%>

	<!-- 	<form id="action_dispatcher_form" action="ActionDispatcherServlet" method="post" role="form"> -->
	<html:form action="/ActionDispatcher">
		<label for="selected_contact">Choose a contact among those found</label>
		<select class="form-control" name="selected_contact" id="selected_contact">
			<%
				for (IContact c : contacts) {
			%>
			<option value="<%=c.getId()%>"><%=DomainUtils.displayContact(c)%></option>
			<%
				}
			%>
		</select>
		<br>
		<label for="wanted_action_radios">Select the wanted action (for add to and remove from a group you need to specifies target groups)</label>
		<div class="form-group" id="wanted_action_radios">
			<input type="radio" name="wanted_action" id="show_details_contact" value="show_details_contact">
			<span class="label label-default">
				<label for="show_details_contact">Show details</label>
			</span>
			<br>
			<input type="radio" name="wanted_action" id="modify_contact" value="modify_contact">
			<span class="label label-primary">
				<label for="modify_contact">Modify</label>
			</span>
			<br>
			<input type="radio" name="wanted_action" id="delete_contact" value="delete_contact">
			<span class="label label-default">
				<label for="delete_contact">Delete</label>
			</span>
			<br>
			<input type="radio" name="wanted_action" id="add_to_contactgroup" value="add_to_contactgroup">
			<span class="label label-primary">
				<label for="add_to_contactgroup">Add to a contact group</label>
			</span>
			<br>
			<input type="radio" name="wanted_action" id="remove_from_contactgroup" value="remove_from_contactgroup">
			<span class="label label-default">
				<label for="remove_from_contactgroup">Remove from a contact group</label>
			</span>
			<br>
			<div class="form-control" id="contactgroups_div" name="contactgroups_div" style="overflow: scroll; height: 150px; width: 200px">
				<%
					String options = "";
								IUser user = (IUser) request.getSession(false)
										.getAttribute(DomainUtils.CURRENT_USER_STRING);

								// 					for (ContactGroup cg : DAOFactory.getDaoContactGroup().getByUserId(
								// 							user.getId())) {
								IDAOContactGroup daoContactGroup = (IDAOContactGroup) context
										.getBean("beanDAOContactGroup");

								for (IContactGroup cg : daoContactGroup
										.getByUserId(user.getId())) {
									options += "<input type=\"checkbox\" name=\"contactgroups\" id=\"chbx_"
											+ cg.getGroupId()
											+ "\" value=\""
											+ cg.getGroupId()
											+ "\"/>"
											+ "<label for=\"chbx_"
											+ cg.getGroupId()
											+ "\">"
											+ cg.getGroupName()
											+ "</label><br>";
								}
				%>
				<%=options%>
			</div>
		</div>


		<br>
		<button type="submit" class="btn btn-default">Submit</button>
		<button type="reset" class="btn btn-default">Reset</button>
	</html:form>
	<%
		}
	%>

	<br>
	<%@include file="/pages/common_style/footer.jsp"%>

</body>
</html:html>